package com.liuheng.spzx.product.utils;

import com.liuheng.spzx.model.entity.product.Category;

import java.util.ArrayList;
import java.util.List;

public class CategoryHelper {
    public static List<Category> getTree(List<Category> categories)
    {
        List<Category> prentTree = new ArrayList<>();
        for (Category category : categories) {
            if (category.getParentId()==0){
                prentTree.add(createTree(category,categories));
            }
        }
        return prentTree;
    }
    public static Category createTree(Category category,List<Category> categories){
        ArrayList<Category> categories1 = new ArrayList<>();
        category.setChildren(categories1);
        for (Category category1 : categories) {
                if ((category1.getParentId()).equals(category.getId())){
                    category.getChildren().add(createTree(category1,categories));
                }
        }
        return category;
    }
}
